//
//  BaseTableViewCell.m
//  MyFunctionDemo
//
//  Created by Ting Han on 2017/9/21.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "BaseTableViewCell.h"

@implementation BaseTableViewCell

//代码
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupCell];
        [self buildSubview];
    }
    return self;
}

//xib/storyboard
- (void)awakeFromNib {
    [super awakeFromNib];
    [self setupCell];
    [self buildSubview];
}
- (void)setupCell {
    
}
- (void)buildSubview {
    
}
- (void)loadContent{
    
}
- (void)selectedEvent{
    
}
+ (CGFloat)cellHeightWithData:(id)data{
    return 0.f;
}

- (void)loadContentWithAdapter:(CellDataAdapter *)dataAdapter
                      delegate:(id <BaseTableViewCellDelegate>)delegate
                     tableView:(UITableView *)tableView
                     indexPath:(NSIndexPath *)indexPath {
    
    _dataAdapter = dataAdapter;
    _data        = dataAdapter.data;
    _indexPath   = indexPath;
    _baseCellDelegate    = delegate;
    _tableView   = tableView;
    [self loadContent];
}
#pragma mark --- 注册cell
+ (void)registerToTableView:(UITableView *)tableView reuseIdentifier:(NSString *)reuseIdentifier {
    
    [tableView registerClass:[self class] forCellReuseIdentifier:reuseIdentifier];
}
+ (void)registerNibToTableView:(UITableView *)tableView reuseIdentifier:(NSString *)reuseIdentifier{
    [tableView registerNib:[UINib nibWithNibName:NSStringFromClass([self class]) bundle:[NSBundle mainBundle]] forCellReuseIdentifier:reuseIdentifier];
}

+ (void)registerToTableView:(UITableView *)tableView {
    [tableView registerClass:[self class] forCellReuseIdentifier:NSStringFromClass([self class])];
}
+ (void)registerNibToTableView:(UITableView *)tableView{
    [tableView registerNib:[UINib nibWithNibName:NSStringFromClass([self class]) bundle:[NSBundle mainBundle]] forCellReuseIdentifier:NSStringFromClass([self class])];
}

@end

@implementation UITableView(BaseCell)
- (BaseTableViewCell *)dequeueReusableCellAndLoadDataWithAdapter:(CellDataAdapter *)dataAdapter indexPath:(NSIndexPath *)indexPath{
    BaseTableViewCell *cell = [self dequeueReusableCellWithIdentifier:dataAdapter.cellReuseIdentifier forIndexPath:indexPath];
    [cell loadContentWithAdapter:dataAdapter delegate:nil tableView:self indexPath:indexPath];
    
    return cell;
}
- (BaseTableViewCell *)dequeueReusableCellAndLoadDataWithAdapter:(CellDataAdapter *)dataAdapter delegate:(id<BaseTableViewCellDelegate>)delegate indexPath:(NSIndexPath *)indexPath{
    BaseTableViewCell *cell = [self dequeueReusableCellWithIdentifier:dataAdapter.cellReuseIdentifier forIndexPath:indexPath];
    [cell loadContentWithAdapter:dataAdapter delegate:delegate tableView:self indexPath:indexPath];
    return cell;
}
- (CGFloat)cellHeightWithAdapter:(CellDataAdapter *)adapter{
    return adapter.cellHeight;
}
@end
